package com.sync.syncmasterdata.product.service.convert;

import com.sync.syncmasterdata.product.pojo.cp.InventoryForCP;
import com.sync.syncmasterdata.product.pojo.erp.InventoryForU8;
import com.sync.syncmasterdata.product.pojo.oa.InventoryForOA;
import com.sync.syncmasterdata.product.repository.u8.ComputationunitRepository;
import com.sync.syncmasterdata.product.service.ConvertInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * oa产品对象 转cp产品对象
 */
@Service
public class ProductU8toCP implements ConvertInterface<InventoryForU8, InventoryForCP> {


    @Autowired
    private ComputationunitRepository computationunitRepository;
    @Override
    public InventoryForCP convertTo(InventoryForU8 u8) {
        InventoryForCP cp = new InventoryForCP();
        cp.setTongyongmc(u8.getCurrencyName());
        cp.setKuwei(u8.getSelf_define4());
        cp.setChanpinlx(u8.getSort_code());
        cp.setCunhuobm(u8.getCode());
        cp.setChanpinmc(u8.getName());
        cp.setHuohao(u8.getInvaddCode());
        cp.setGuigexh(u8.getSpecs());
        cp.setZhujildw(u8.getMain_measure());
        cp.setJiliangdwwb(getUnitName(u8.getMain_measure()));    //计量单位文本
        cp.setPinpaiwb(u8.getSelf_define5());
        cp.setShengchanqy(u8.getMain_supplier());
        cp.setShengchanqywb(u8.getEnterprise());
        cp.setShifoucgbh(integerToBool(0)); //默认都为0
//        cp.setJiancexmh(oa.getJiancexmh())  //检测项目号;

        //需转换
        cp.setGuochanjk(covguochanpp(u8.getBImport()));
        cp.setYewulx(u8.getCidefine2());
        cp.setChanpinx(u8.getCidefine3());
        cp.setChanpinz(u8.getCidefine4());
//        cp.setXiangmumc(oa.getXiangmumc());   转换的没有id
        cp.setXiangmumcwb(u8.getCidefine5());
        cp.setShifouylqx(covshifouylqx(u8.getSelf_define2()));
        //药监专用
//        cp.setShifoucw(integerToBool(oa.getShifoucw()));
//        cp.setTingshou(integerToBool(oa.getTingshou()));
//        cp.setWenduc(oa.getWenduc());
//        cp.setWenduz(oa.getWenduz());
//        cp.setYujingyxq(oa.getYujingyxq());
//        cp.setYouxiaoq(oa.getYouxiaoq());
//        cp.setYanghulx(covyanghulx(oa.getYanghulx()));
        return cp;
    }




    /**
     * 转换国产 进口
     */
    private String covguochanpp(int i) {
        try {
            if (i == 0) {
                return "国产";
            } else {
                return "进口";
            }
        } catch (Exception e) {
//            e.printStackTrace();
            return "进口";
        }

    }
    /**
     * 是否医疗器械转换
     * @param s
     * @return
     */
    private boolean covshifouylqx(String s) {
        try {
            if(s.equals("否")){
                return false;
            }else{
                return true;
            }
        } catch (Exception e) {
            return false;
        }

    }

    /**
     * 是否常温转换——是否停售 转换 0=否 1=是
     * @param i
     * @return
     */
    private boolean integerToBool(Integer i) {
        try {
            if(i==1){
                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
//            e.printStackTrace();
            return false;
        }
    }

    /**
     * 养护类型转换
     * @param i
     * @return
     */
    private String covyanghulx(Integer i) {
        try {
            if (i == null) {
                return "";
            } else if (i == 1) {
                return "重点养护";
            } else if (i == 2) {
                return "一般养护";
            }else{
                return "一般养护";
            }

        } catch (Exception e) {
//            e.printStackTrace();
            return "一般养护";
        }
    }

    private String getUnitName(String code) {
        return computationunitRepository.getUnitName(code);
    }
}
